package com.albert.core.controller.api;


import com.albert.base.utils.JwtUtils;
import com.albert.common.result.R;
import com.albert.core.hfb.RequestHelper;
import com.albert.core.pojo.vo.UserBindVO;
import com.albert.core.service.UserBindService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author Hikari
 * @since 2024-07-21
 */
@RestController
@Api(tags="会员账号绑定")
@Slf4j
@RequestMapping("/api/core/userBind")
public class UserBindController {
    @Resource
    private UserBindService bindService;
    @ApiOperation("账户绑定提交数据")
    @PostMapping("/auth/bind")
    public R bind(@RequestBody UserBindVO bindVO, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String fromStr = bindService.commitBindUser(bindVO,userId);
        return R.ok().data("formStr",fromStr);
    }

    @ApiOperation("账户绑定异步回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        log.info("开始执行异步回调方法notify");
        Map<String, Object> stringMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("用户绑定接受的参数如下:{}", JSON.toJSONString(stringMap));
        //检验签名
        if(!RequestHelper.isSignEquals(stringMap)) {
            log.error("用户账号绑定异步回调签名错误：" + JSON.toJSONString(stringMap));
            return "fail";
        }
        //修改绑定状态
        bindService.notify(stringMap);
        return "success";
    }



}

